package com.yanyeori.screw;

import cn.smallbun.screw.core.Configuration;
import cn.smallbun.screw.core.engine.EngineConfig;
import cn.smallbun.screw.core.engine.EngineFileType;
import cn.smallbun.screw.core.engine.EngineTemplateType;
import cn.smallbun.screw.core.execute.DocumentationExecute;
import cn.smallbun.screw.core.process.ProcessConfig;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * screw数据库文档生成工具
 *
 * @author chenkuan
 * @since 2021/11/18 018 11:17
 */
public class ScrewMain {

    public static void main(String[] args) {
        new ScrewMain().documentGeneration();
    }

    //数据库驱动
    private String driverClassName = "com.mysql.cj.jdbc.Driver";
    //数据库url
    private String jdbcUrl = "jdbc:mysql://127.0.0.1/clear?characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai";
    //数据库用户名
    private String username = "root";
    //数据库密码
    private String password = "root";

    //生成文件路径
    private String fileOutputDir = "D://test";
    //生成文件名称
    private String fileName = "数据库文档";
    //生成文件类型 HTML、WORD、MD
    private EngineFileType fileType = EngineFileType.HTML;
    //生成文件后是否自动打开文件夹
    private boolean openOutputDir = true;

    //忽略表名
    private List<String> ignoreTableName = Arrays.asList();
    //忽略表前缀
    private List<String> ignorePrefix = Arrays.asList();
    //忽略表后缀
    private List<String> ignoreSuffix = Arrays.asList();

    //文档版本
    private String version = "1.0.0";
    //文档描述
    private String description = "数据库文档";

    /**
     * 文档生成
     */
    private void documentGeneration() {
        //数据源
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setDriverClassName(driverClassName);
        hikariConfig.setJdbcUrl(jdbcUrl);
        hikariConfig.setUsername(username);
        hikariConfig.setPassword(password);
        //设置可以获取tables remarks信息
        hikariConfig.addDataSourceProperty("useInformationSchema", "true");
        hikariConfig.setMinimumIdle(2);
        hikariConfig.setMaximumPoolSize(5);
        DataSource dataSource = new HikariDataSource(hikariConfig);
        //生成配置
        EngineConfig engineConfig = EngineConfig.builder()
                //生成文件路径
                .fileOutputDir(fileOutputDir)
                //打开目录
                .openOutputDir(openOutputDir)
                //文件类型
                .fileType(fileType)
                //生成模板实现
                .produceType(EngineTemplateType.freemarker)
                //自定义文件名称
                .fileName(fileName).build();

        ProcessConfig processConfig = ProcessConfig.builder()
                //指定生成逻辑、当存在指定表、指定表前缀、指定表后缀时，将生成指定表，其余表不生成、并跳过忽略表配置
                //根据名称指定表生成
                .designatedTableName(new ArrayList<>())
                //根据表前缀生成
                .designatedTablePrefix(new ArrayList<>())
                //根据表后缀生成
                .designatedTableSuffix(new ArrayList<>())
                //忽略表名
                .ignoreTableName(ignoreTableName)
                //忽略表前缀
                .ignoreTablePrefix(ignorePrefix)
                //忽略表后缀
                .ignoreTableSuffix(ignoreSuffix).build();
        //配置
        Configuration config = Configuration.builder()
                //版本
                .version(version)
                //描述
                .description(description)
                //数据源
                .dataSource(dataSource)
                //生成配置
                .engineConfig(engineConfig)
                //生成配置
                .produceConfig(processConfig)
                .build();
        //执行生成
        new DocumentationExecute(config).execute();
    }
}
